package myJs.user

import myJs.Codecs._
import myJs.myPkg.jquery.{$, JQuery, JQueryAjaxSettings, JQueryXHR}
import myJs.user.SampleDetail._
import scalatags.Text.all._
import shared.VarTool._

import scala.scalajs.js
import scala.scalajs.js.JSON
import argonaut._
import Argonaut._
import myJs.myPkg.{PlotlyConfigOptions, myPlotly}
import org.scalajs.dom.ext.Ajax
import shared.plotly.{Bar, Pie}
import shared.plotly.Sequence.{Doubles, Strings}
import shared.plotly.element.Anchor.Top
import shared.plotly.element.Color.StringColor
import shared.plotly.element.{Anchor, AxisType, HoverInfo, Marker}
import shared.plotly.layout.HoverMode.Closest
import shared.plotly.layout.Ref.{Paper, XRef}
import shared.plotly.layout.{Annotation, Axis, Font, Layout, Margin}

import scala.concurrent.ExecutionContext.Implicits.global
import shared.Pojo._
import myJs.Implicits._
import myJs.tool.Pojo.GenotypeData
import myJs.tool.Tool
import shared.VarTool
import scala.scalajs.js.Dynamic.{global => g}

/**
 * Created by Administrator on 2020/1/16
 */
object SampleSnpDetail {

  val snpId = "snp"
  val tableId="dataTable"

  def str2SnpReadsDatas(rs: String) = {
    rs.decodeOption[List[SnpReadsData]].getOrElse(Nil)
  }

  def getSnpReadsData(idStr: String) = {
    val url = g.jsRoutes.controllers.SampleController.getSnpReadsData().url.toString
    Ajax.get(url = s"${url}?id=${idStr}").map { xhr =>
      str2SnpReadsDatas(xhr.responseText)
    }

  }

  def snpShow(sample: JDJAn, datas: List[SnpReadsData]) = {
    val filterDatas = datas
    Tool.fillSnpReadsData(sample, filterDatas, $(s"#${
      snpId
    } #${tableId}"))

  }

  def snpDetailPlot(seqDatas: List[CurSeqData]) = {
    val xs = seqDatas.map(_.genotype)
    val reads = seqDatas.map(_.reads)
    val plotData = Seq(
      Pie(
        labels = xs,
        values = reads,
        marker = Marker(
        ),
        hoverinfo = HoverInfo.Y,
        width = 0.5
      )
    )

    val layout = Layout(
      title = s"",
      xaxis = Axis(
        title = s"",
        `type` = AxisType.Category,
        tickangle = 0.0,
        autorange = true,
        tickfont = Font(size = 10)
      ),
      yaxis = Axis(
        title = s"Number of reads",
        autorange = true
      ),
      margin = Margin(b = 0,
        t = 0,
        l = 0,
        r = 0),
      width = 400,
      height = 400,
      hovermode = Closest,
      dragmode = false
    )
    val config = PlotlyConfigOptions.displayModeBar(false)
    myPlotly.newPlot($("#snpModal #detailChart"), plotData, layout, config)
  }

}
